package com.my.main.base.controller;

import com.my.main.base.entity.response.ProfileResult;
import com.my.main.common.utils.IdWorker;
import com.my.main.common.utils.RequestUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BaseController {

    /**id生成器*/
    @Autowired
    public IdWorker idWorker;
    /**获取配置文件*/
    @Autowired
    public Environment env;

    /** 容器 */
    public HttpServletRequest request;
    public HttpServletResponse response;

    /** 权限部分参数 */
    public ProfileResult userInfo;


    /** 工具对象 */
    protected Log logger = LogFactory.getLog(getClass());
    public RequestUtil requestUtil;

    // 不拦截
    public static final String NOINTERCEPT = "NOINTERCEPT";

    //使用shiro获取
    @ModelAttribute
    public void setResAnReq(HttpServletRequest request,HttpServletResponse response) {
        this.request = request;
        this.response = response;
        requestUtil = new RequestUtil(request);

        //获取session中的安全数据
        Subject subject = SecurityUtils.getSubject();
        //1.subject获取所有的安全数据集合
        PrincipalCollection principals = subject.getPrincipals();
        if(principals != null && !principals.isEmpty()){
            //2.获取安全数据
            this.userInfo  = (ProfileResult)principals.getPrimaryPrincipal();
        }
    }
}
